package com.ysd.lis.service.self.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.slef.FunctionParam;
import com.ysd.lis.entity.lab.LabBarCodeType;
import com.ysd.lis.entity.lab.LabFeeItem;
import com.ysd.lis.entity.lab.LabRptVsBarcode;
import com.ysd.lis.entity.slef.SelfFunction;
import com.ysd.lis.entity.slef.SelfMachineFunction;
import com.ysd.lis.mapper.self.SelfFunctionMapper;
import com.ysd.lis.mapper.self.SelfMachineFunctionMapper;
import com.ysd.lis.service.self.SelfMachineFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-05-13
 */
@Service
public class SelfMachineFunctionServiceImpl extends ServiceImpl<SelfMachineFunctionMapper, SelfMachineFunction> implements SelfMachineFunctionService {

    @Autowired
    private SelfFunctionMapper selfFunctionMapper;
    @Override
    public Result addMachineFunction(SelfMachineFunction selfMachineFunction) {
        boolean save = this.save(selfMachineFunction);
        if (save) {
            return Result.succ(1, "", null);
        } else {
            return Result.fail("添加失败！");
        }
    }

    @Override
    public Result findFunction(FunctionParam functionParam) {

        MPJLambdaWrapper<SelfFunction> lambdaQueryWrapper = new MPJLambdaWrapper<>();

        lambdaQueryWrapper.selectAll(SelfFunction.class);
        if (StrUtil.isNotEmpty(functionParam.getKeywords())) {
            lambdaQueryWrapper.and(wrapper ->
                    wrapper.like(SelfFunction::getFuncName, functionParam.getKeywords())
                            .or()
                            .like(SelfFunction::getFuncCode, functionParam.getKeywords())
                            .or()
                            .like(SelfFunction::getTitle, functionParam.getKeywords())
            );
        }
        if (StrUtil.isNotEmpty(functionParam.getSuid())) {
            StringBuffer existsSql = new StringBuffer("SELECT 1 FROM SELF_MACHINE_FUNCTION t2 WHERE t.FUNC_CODE = t2.FUNC_CODE and t2.SUID = '" + functionParam.getSuid() + "'");// AND t2.BARCODE_NO = '" + feeItemPageParam.getBarcodeNo() + "' ");
            lambdaQueryWrapper.notExists(existsSql.toString());
        }
        //lambdaQueryWrapper.eq(StringUtils.isNotBlank(feeItemPageParam.getOrgId()), LabFeeItem::getOrgId, feeItemPageParam.getOrgId());
        List<SelfFunction> selfFunctions = selfFunctionMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", selfFunctions);
    }

    @Override
    public Result findMachineFunction(String suid) {
        MPJLambdaWrapper<SelfMachineFunction> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(SelfMachineFunction.class)
                .selectAs(SelfFunction::getFuncName, SelfMachineFunction::getFuncName)
                .selectAs(SelfFunction::getTitle, SelfMachineFunction::getTitle)
                .leftJoin(SelfFunction.class, SelfFunction::getFuncCode, SelfFunction::getFuncCode)
                .eq(SelfMachineFunction::getSuid, suid)
                .eq(SelfMachineFunction::getDelFlag, 0)
                .eq(SelfFunction::getDelFlag, 0)
                .orderByAsc(SelfFunction::getSeq);
        List<SelfMachineFunction> labBarCodeTypes = this.list(mpjLambdaWrapper);
        return Result.succ(1, "查询成功", labBarCodeTypes);
    }

    @Override
    public Result deleteMachineFunction(SelfMachineFunction selfMachineFunction) {
        boolean i = this.removeById(selfMachineFunction.getId());
        if (i) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败！");
        }
    }
}
